/*
 *  * Copyright (C) 2021 Huawei Device Co., Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.hanks.animatecheckbox.slice;

import com.hanks.animatecheckbox.ResourceTable;
import com.hanks.library.AnimateCheckBox;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainAbilitySlice extends AbilitySlice {

    private List<Demo> dataList = new ArrayList<>();
    private Set<Demo> checkedSet = new HashSet<>();
    private Context mContext;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mContext = getApplicationContext();

        bindDatas();
        bindViews();
    }

    private void bindDatas() {
        for (int i = 0; i < 100; i++) {
            Demo demo = new Demo();
            demo.setContent("this is a simple item : "+ i);
            dataList.add(demo);
        }

    }

    private void bindViews() {
        ListContainer listView = (ListContainer) findComponentById(ResourceTable.Id_listView);
        listView.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public int getItemComponentType(int position) {
                return position;
            }

            @Override
            public Component getComponent(int position, Component convertView, ComponentContainer container) {
                if (convertView == null) {
                    convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_list_demo, container, false);
                }

                Text text = (Text) convertView.findComponentById(ResourceTable.Id_text);
                final AnimateCheckBox checkBox = (AnimateCheckBox) convertView.findComponentById(ResourceTable.Id_checkbox);

                final Demo item = dataList.get(position);
                text.setText(item.getContent());
                if(checkedSet.contains(item)){
                    checkBox.setChecked(true);
                }else {
                    //checkBox.setChecked(false); //has animation
                    checkBox.setUncheckStatus();
                }
                checkBox.setOnCheckedChangeListener(new AnimateCheckBox.OnCheckedChangeListener() {
                    @Override public void onCheckedChanged(Component buttonView, boolean isChecked) {
                        if (isChecked) {
                            checkedSet.add(item);
                        } else {
                            checkedSet.remove(item);
                        }
                    }
                });

                return convertView;
            }

        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
